import {Component, OnInit, Output, EventEmitter} from '@angular/core';
import {ROUTE} from "../../service/route.service";
import {LocalStorage} from "../../app.local-storage";
import {UserModel} from "../../model/user.model";
import {CONSTANT} from "../../service/constant.service";
import {UserInfo} from "../../service/userinfo.service";
import {USER} from "../../service/user.service";
import {CONTRACT} from "../../service/contract.service";
import {USERAPI} from "../../service/user.api.service";

@Component({
  selector: 'app-personal',
  templateUrl: 'personal.component.html',
  styleUrls: ['personal.component.scss']
})
export class PersonalComponent implements OnInit {


  user:UserModel;
  pensonalInfo:any = {};
  hello:string;
  menus:any = this.constant.personalList;
  contractList:any = [];
  currentContract:any = {}
  currentId:string;

  constructor(private route:ROUTE,
              private USER:USER,
              private USERAPI:USERAPI,
              private constant:CONSTANT,
              private contractApi:CONTRACT,
              private ls:LocalStorage,
              private getUser:UserInfo) {
    this.hello = this.constant.getNewDate();

  }
  changeHouse(){
    this.currentContract.id = this.currentId
    for(let v of this.contractList){
      if (v.id == this.currentContract.id){
        this.currentContract.remainingDays = v.remainingDays
      }
    }
    console.log(this.currentContract.remainingDays);
    this.USER.refreshC(this.currentContract);
  }

  ngOnInit() {

    this.user = this.USER.get();
    this.USER.refreshUser.subscribe(v=> {
      console.log("USER.refresh");
      this.user = this.USER.get();
      this.contractApi.getContractList(v=>{
        this.contractList = []
        for(let item of v.content){
          if(item.status == 8){
            this.contractList.push(item) ;
          }
        }
        this.currentContract.id= this.contractList[0].id
        this.currentContract.remainingDays= this.contractList[0].remainingDays
        this.currentId =this.currentContract.id
        this.USER.refreshC(this.currentContract);
      })
    })
    this.contractApi.getContractList(v=>{
      this.contractList = []
      for(let item of v.content){
        if(item.status == 8){
          this.contractList.push(item) ;
        }
      }
      this.currentContract.id= this.contractList[0].id
      this.currentContract.remainingDays= this.contractList[0].remainingDays
      this.currentId =this.currentContract.id
      this.USER.refreshC(this.currentContract);
    })
    this.USERAPI.fetchUser(v=>{
      this.USER.update(new UserModel(v))
    })
  }

  goToMyBalanceView() {
    this.route.goBalanceView();
  }

  goToMyScoreView() {
    this.route.goScoreView();
  }


}
